﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Genesis
{
    class Settings
    {
        public static void Save(string path)
        {
            if (Directory.Exists(path))
            {
                if (Directory.Exists(path + @"\Config"))
                {
                    Internal_Settings.Save(path + @"\Config\config.gsf");
                    Visual_Settings.Save(path + @"\Config\visual.gsf");
                }
            }
        }
        public static void Load(string path)
        {
            if (Directory.Exists(path))
            {
                if (Directory.Exists(path + @"\Config"))
                {
                    if (File.Exists(path + @"\Config\config.gsf"))
                    {
                        Internal_Settings.Load(path + @"\Config\config.gsf");
                    }
                    else
                    {
                        Internal_Settings.DefaultSettings();
                        Internal_Settings.Save(path + @"\Config\config.gsf");
                    }
                    if (File.Exists(path + @"\Config\visual.gsf"))
                    {
                        Visual_Settings.Load(path + @"\Config\visual.gsf");
                    }
                    else
                    {
                        Visual_Settings.DefaultSettings();
                        Visual_Settings.Save(path + @"\Config\visual.gsf");
                    }

                }
                else
                {
                    Directory.CreateDirectory(path + @"\Config");
                    Internal_Settings.DefaultSettings();
                    Internal_Settings.Save(path + @"\Config\config.gsf");
                    Visual_Settings.DefaultSettings();
                    Visual_Settings.Save(path + @"\Config\visual.gsf");
                }
            }
            
            else
            {
                Directory.CreateDirectory(path + @"\Config");
                Internal_Settings.DefaultSettings();
                Internal_Settings.Save(path + @"\Config\config.gsf");
                Visual_Settings.DefaultSettings();
                Visual_Settings.Save(path + @"\Config\visual.gsf");
            }
        }
    }
    class Visual_Settings
    {
        public static void Save(string path)
        {
            Visual_ExternalSettings eSettings = new Visual_ExternalSettings();

            eSettings.ButtonStyle = Visual_Settings.ButtonStyle;
            eSettings.ForeColor = Visual_Settings.ForeColor;
            eSettings.BackColor = Visual_Settings.BackColor;
            eSettings.AltBackColor = Visual_Settings.AltBackColor;
            eSettings.NameWidth = Visual_Settings.NameWidth;
            eSettings.ArtistWidth = Visual_Settings.ArtistWidth;
            eSettings.AlbumWidth = Visual_Settings.AlbumWidth;
            eSettings.GenreWidth = Visual_Settings.GenreWidth;
            eSettings.TimeWidth = Visual_Settings.TimeWidth;
            eSettings.PlayWidth = Visual_Settings.PlayWidth;

            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stream, eSettings);
            stream.Close();
        }
        public static void Load(string path)
        {
            Visual_ExternalSettings eSettings = new Visual_ExternalSettings();
            Stream stream = File.Open(path, FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();

            eSettings = (Visual_ExternalSettings)bf.Deserialize(stream);
            stream.Close();

            Visual_Settings.ButtonStyle = eSettings.ButtonStyle;
            Visual_Settings.ForeColor = eSettings.ForeColor;
            Visual_Settings.BackColor = eSettings.BackColor;
            Visual_Settings.AltBackColor = eSettings.AltBackColor;
            Visual_Settings.NameWidth = eSettings.NameWidth;
            Visual_Settings.ArtistWidth = eSettings.ArtistWidth;
            Visual_Settings.AlbumWidth = eSettings.AlbumWidth;
            Visual_Settings.GenreWidth = eSettings.GenreWidth;
            Visual_Settings.TimeWidth = eSettings.TimeWidth;
            Visual_Settings.PlayWidth = eSettings.PlayWidth;
        }

        public static void DefaultSettings()
        {
            ButtonStyle = DefaultButtonStyle;
            ForeColor = DefaultForeColor;
            BackColor = DefaultBackColor;
            AltBackColor = DefaultAltBackColor;

            NameWidth = DefaultNameWidth;
            ArtistWidth = DefaultArtistWidth;
            AlbumWidth = DefaultAlbumWidth;
            GenreWidth = DefaultGenreWidth;
            TimeWidth = DefaultTimeWidth;
            PlayWidth = DefaultPlayWidth;
        }

        public static int DefaultButtonStyle = 0;
        public static Color DefaultForeColor = Color.Black;
        public static Color DefaultBackColor = Color.White;
        public static Color DefaultAltBackColor = Color.WhiteSmoke;

        public static int DefaultNameWidth = 150;
        public static int DefaultArtistWidth = 145;
        public static int DefaultAlbumWidth = 135;
        public static int DefaultGenreWidth = 85;
        public static int DefaultTimeWidth = 60;
        public static int DefaultPlayWidth = 65;

        public static int NameWidth
        {
            get { return namewidth; }
            set { namewidth = value; }
        }
        private static int namewidth;

        public static int ArtistWidth
        {
            get { return artistwidth; }
            set { artistwidth  = value; }
        }
        private static int artistwidth;

        public static int AlbumWidth
        {
            get { return albumwidth; }
            set { albumwidth  = value; }
        }
        private static int albumwidth;

        public static int GenreWidth
        {
            get { return genrewidth; }
            set { genrewidth  = value; }
        }
        private static int genrewidth;

        public static int TimeWidth
        {
            get { return timewidth; }
            set { timewidth = value; }
        }
        private static int timewidth;

        public static int PlayWidth
        {
            get { return playwidth; }
            set { playwidth = value; }
        }
        private static int playwidth;

        public static int ButtonStyle
        {
            get { return buttonstyle; }
            set { buttonstyle = value; }
        }
        private static int buttonstyle;

        public static Color ForeColor
        {
            get { return forecolor; }
            set { forecolor = value; }
        }
        private static Color forecolor;

        public static Color BackColor
        {
            get { return backcolor; }
            set { backcolor = value; }
        }
        private static Color backcolor;

        public static Color AltBackColor
        {
            get { return altBackColor; }
            set { altBackColor = value; }
        }
        private static Color altBackColor;
    }
    class Internal_Settings
    {
        public static void Save(string path)
        {
            Internal_ExternalSettings iSettings = new Internal_ExternalSettings();

            iSettings.iTunesLibraryPath = Internal_Settings.iTunesLibraryPath;
            iSettings.Volume = Internal_Settings.Volume;
            iSettings.Shuffle = Internal_Settings.Shuffle;
            iSettings.RepeatState = Internal_Settings.RepeatState;

            Stream stream = File.Open(path, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stream, iSettings);
            stream.Close();
        }
        public static void Load(string path)
        {
            Internal_ExternalSettings iSettings = new Internal_ExternalSettings();
            Stream stream = File.Open(path, FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            
            iSettings = (Internal_ExternalSettings)bf.Deserialize(stream);

            Internal_Settings.iTunesLibraryPath = iSettings.iTunesLibraryPath;
            Internal_Settings.Volume = iSettings.Volume;
            Internal_Settings.Shuffle = iSettings.Shuffle;
            Internal_Settings.RepeatState = iSettings.RepeatState;
        }
        public static void DefaultSettings()
        {
            iTunesLibraryPath = DefaultiTunesLibraryPath;
            Volume = DefaultVolume;
            Shuffle = DefaultShuffle;
            RepeatState = RepeatState;
        }

        public static int DefaultVolume = 100;
        public static bool DefaultShuffle = false;
        public static int DefaultRepeatState = 0;
        public static string DefaultiTunesLibraryPath = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) + "\\iTunes\\iTunes Music Library.xml";

        public static string iTunesLibraryPath
        {
            get { return ituneslibrarypath; }
            set { ituneslibrarypath = value; }
        }
        private static string ituneslibrarypath;

        public static int Volume
        {
            get { return volume; }
            set { volume = value; }
        }
        private static int volume;

        public static bool Shuffle
        {
            get { return shuffle; }
            set { shuffle = value; }
        }
        private static bool shuffle;
        
        public static int RepeatState
        {
            get { return repeatstate; }
            set { repeatstate = value; }
        }
        private static int repeatstate;
    }

    [Serializable()]
    class Visual_ExternalSettings : ISerializable
    {
        public int ButtonStyle;
        public Color ForeColor;
        public Color BackColor;
        public Color AltBackColor;
        public int NameWidth;
        public int ArtistWidth;
        public int AlbumWidth;
        public int GenreWidth;
        public int TimeWidth;
        public int PlayWidth;

        public Visual_ExternalSettings()
        {
                ForeColor = Visual_Settings.ForeColor;
                BackColor = Visual_Settings.BackColor;
                AltBackColor = Visual_Settings.AltBackColor;
                NameWidth = Visual_Settings.NameWidth;
                ArtistWidth = Visual_Settings.ArtistWidth;
                AlbumWidth = Visual_Settings.AlbumWidth;
                GenreWidth = Visual_Settings.GenreWidth;
                TimeWidth = Visual_Settings.TimeWidth;
                PlayWidth = Visual_Settings.PlayWidth;
        }


        // Deserialization
        public Visual_ExternalSettings(SerializationInfo info, StreamingContext context)
        {
            ButtonStyle = (int)info.GetValue("ButtonStyle", typeof(int));
            ForeColor = (Color)info.GetValue("ForeColor", typeof(Color));
            BackColor = (Color)info.GetValue("BackColor", typeof(Color));
            AltBackColor = (Color)info.GetValue("AltBackColor", typeof(Color));
            NameWidth = (int)info.GetValue("NameWidth", typeof(int));
            ArtistWidth = (int)info.GetValue("ArtistWidth", typeof(int));
            AlbumWidth = (int)info.GetValue("AlbumWidth", typeof(int));
            GenreWidth = (int)info.GetValue("GenreWidth", typeof(int));
            TimeWidth = (int)info.GetValue("TimeWidth", typeof(int));
            PlayWidth = (int)info.GetValue("PlayWidth", typeof(int));
        }
        // Serialization
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ButtonStyle", ButtonStyle);
            info.AddValue("ForeColor", ForeColor);
            info.AddValue("BackColor", BackColor);
            info.AddValue("AltBackColor", AltBackColor);
            info.AddValue("NameWidth", NameWidth);
            info.AddValue("ArtistWidth", ArtistWidth);
            info.AddValue("AlbumWidth", AlbumWidth);
            info.AddValue("GenreWidth", GenreWidth);
            info.AddValue("TimeWidth", TimeWidth);
            info.AddValue("PlayWidth", PlayWidth);

        }
    }
    [Serializable()]
    class Internal_ExternalSettings : ISerializable
    {
        public string iTunesLibraryPath;
        public int Volume;
        public bool Shuffle;
        public int RepeatState;

        public Internal_ExternalSettings()
        {
            if (Internal_Settings.iTunesLibraryPath != null)
                iTunesLibraryPath = Internal_Settings.iTunesLibraryPath;
            else
                iTunesLibraryPath = Internal_Settings.DefaultiTunesLibraryPath;

                Volume = Internal_Settings.Volume;
                Shuffle = Internal_Settings.Shuffle;
                RepeatState = Internal_Settings.RepeatState;
        }

        // Deserialization
        public Internal_ExternalSettings(SerializationInfo info, StreamingContext context)
        {
            iTunesLibraryPath = (string)info.GetValue("iTunesLibraryPath", typeof(string));
            Volume = (int)info.GetValue("Volume", typeof(int));
            Shuffle = (bool)info.GetValue("Shuffle", typeof(bool));
            RepeatState = (int)info.GetValue("RepeatState", typeof(int));
        }
        // Serialization
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("iTunesLibraryPath", iTunesLibraryPath);
            info.AddValue("Volume", Volume);
            info.AddValue("Shuffle", Shuffle);
            info.AddValue("RepeatState", RepeatState);
        }
    }
}
